package com.banyou.banyou.order.journey;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banyou.banyou.R;
import com.banyou.banyou.base.BaseActivity;
import com.banyou.banyou.bean.journey.info.UserOrderInfoBean;
import com.banyou.banyou.order.evaluate.EvaluateActivity;
import com.banyou.banyou.order.evaluatehistory.EvaluateHistoryActivity;
import com.banyou.banyou.util.Constants;
import com.banyou.banyou.util.DialogFactory;
import com.banyou.banyou.util.FontDisplayUtil;
import com.banyou.banyou.util.GlideUtil;
import com.banyou.banyou.util.RxBus;
import com.banyou.banyou.util.ShowToast;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 类描述：游客，查看订单详情
 * 创建人：Random
 * 修改时间：
 * 修改备注：
 */

public class JourneyInfoActivity extends BaseActivity<JourneyInfoPresenter> implements JourneyInfoContract.View {

    @BindView(R.id.back_btn)
    ImageView mBackBtn;
    @BindView(R.id.title_bar)
    RelativeLayout mTitleBar;
    @BindView(R.id.main_img)
    ImageView mMainImg;
    @BindView(R.id.name)
    TextView mName;
    @BindView(R.id.sex)
    TextView mSex;
    @BindView(R.id.age)
    TextView mAge;
    @BindView(R.id.star_content)
    LinearLayout mStarContent;
    @BindView(R.id.short_msg)
    TextView mShortMsg;
    @BindView(R.id.head_content)
    RelativeLayout mHeadContent;
    @BindView(R.id.origin_time)
    TextView mOriginTime;
    @BindView(R.id.total_price)
    TextView mTotalPrice;
    @BindView(R.id.order_state)
    TextView mOrderState;
    @BindView(R.id.cancle_btn)
    Button mCancleBtn;
    @BindView(R.id.end_btn)
    Button mEndBtn;
    @BindView(R.id.order_remark)
    TextView mOrderRemark;
    @BindView(R.id.insurance_price)
    TextView mInsurance;
    @BindView(R.id.daily_price)
    TextView mDailyPrice;
    @BindView(R.id.checkout_speak)
    Button mCheckoutSpeak;

    private long mOrderId;
    private String mStringOrderState;
    private String mCOrderState, mUserOrderState, mRelUserOrderState;

    @Override
    protected JourneyInfoPresenter initPresenter() {
        return new JourneyInfoPresenter();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_journey_info;
    }

    @Override
    protected View initLayoutView() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter.initPresenter(this, new JourneyInfoModel());
        ButterKnife.bind(this);
        mOrderId = getIntent().getLongExtra("orderId", 0);
        mStringOrderState = getIntent().getStringExtra("orderState");
        KLog.a("orderState=" + mStringOrderState);
        mPresenter.getData(mOrderId + "");
        if (mStringOrderState.equals("1")) { //如果是未付款，则付款
            mEndBtn.setText("付款");
        }
        if(!mStringOrderState.equals("5")){
            mCheckoutSpeak.setVisibility(View.GONE);
        }
    }

    @Override
    protected String pageName() {
        return "行程详情";
    }


    @OnClick({R.id.back_btn, R.id.cancle_btn, R.id.end_btn,R.id.checkout_speak})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);
                this.finish();
                break;
            case R.id.cancle_btn:
                cancle();
                break;
            case R.id.end_btn:
                if (mStringOrderState.equals("1")) {
                    pay();//如果是未付款，则进入付款界面
                } else if (mStringOrderState.equals("5")) {
                    jumpToEvaluate();
                } else {
                    end();
                }
                break;
            case R.id.checkout_speak:
                Intent intent=new Intent(this, EvaluateHistoryActivity.class);
                intent.putExtra("orderId",mOrderId);
                startActivity(intent);
                break;
        }
    }

    /**
     * 进入付款界面
     */
    private void pay() {
        Intent intent = new Intent();
        intent.putExtra("orderId", mOrderId);
        startActivity(intent);
    }

    AlertDialog mCancleDialog, mEndDialog;

    /**
     * 取消订单
     */
    private void cancle() {
        DialogFactory factory = new DialogFactory(this);
        factory.setCancleAble(true);
        if (mStringOrderState.equals("1")) {
            mCancleDialog = factory.createDialog("确定取消该订单么？");
        } else {
            mCancleDialog = factory.createDialog("确定取消该订单么，您将会被扣除违约金");
        }
        factory.setSureBtnText("确定");
        factory.setCancleBtnText("取消");
        factory.setOnBtnClickListener(new DialogFactory.OnBtnClickListener() {
            @Override
            public void onSureClick(View view) {
                mCancleDialog.dismiss();
                mPresenter.cancle(mOrderId + "");
            }

            @Override
            public void onCancleClick(View view) {
                mCancleDialog.dismiss();

            }
        });
        mCancleDialog.show();
    }

    /**
     * 结束订单
     */
    private void end() {
        DialogFactory factory = new DialogFactory(this);
        factory.setCancleAble(true);
        mEndDialog = factory.createDialog("确定结束么");
        factory.setSureBtnText("确定");
        factory.setCancleBtnText("取消");
        factory.setOnBtnClickListener(new DialogFactory.OnBtnClickListener() {
            @Override
            public void onSureClick(View view) {
                mEndDialog.dismiss();
                mPresenter.finish(mOrderId + "", getSharedPreference().getString("userType", ""));

            }


            @Override
            public void onCancleClick(View view) {
                mEndDialog.dismiss();

            }
        });
        mEndDialog.show();
    }

    /**
     * 加载数据成功
     *
     * @param bean
     */
    @Override
    public void getDataSuccess(UserOrderInfoBean bean) {
        mCOrderState = bean.getOrderInfo().getOrderState();
        mUserOrderState = bean.getOrderInfo().getUserOrderState();
        mRelUserOrderState = bean.getOrderInfo().getRelUserOrderState();

        //判断订单结束就显示评论按钮
        if (mCOrderState.equals("5")) {
            mEndBtn.setText("点击评价");
        }

        GlideUtil.circleLocalLoad(this, bean.getOrderInfo().getPORTAL_IMAG_PATH(), mMainImg);
        mName.setText("姓名：" + bean.getOrderInfo().getUserName());
        mInsurance.setText("保险费用：" + bean.getOrderInfo().getInsuranceBalance());
        if (bean.getOrderInfo().getSex().equals("M")) {
            mSex.setText("性别：男");
        } else {
            mSex.setText("性别：女");
        }
        mStarContent.removeAllViews();
        for (int i = 0; i < bean.getOrderInfo().getLevel(); i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new LinearLayoutCompat.LayoutParams(FontDisplayUtil.dip2px(15), FontDisplayUtil.dip2px(15)));
            imageView.setImageResource(R.drawable.star);
            mStarContent.addView(imageView);
        }
        if(bean.getOrderInfo().getReward()!=null) {
            mDailyPrice.setText("伴游费用：" + bean.getOrderInfo().getReward() + "￥/天");
        }else{
            mDailyPrice.setText("伴游费用：0￥/天");
        }
        mInsurance.setText("保险费用：" + bean.getOrderInfo().getInsuranceBalance() + "￥");
        mShortMsg.setText("城市：" + bean.getOrderInfo().getRegionName());
        mAge.setText("年龄：" + bean.getOrderInfo().getAGE());
        mOriginTime.setText("订单日程：\n" + bean.getOrderInfo().getORDER_DATES());
        mTotalPrice.setText("费用总计：" + bean.getOrderInfo().getTotalPrice() + "￥");
        mOrderRemark.setText("订单备注：" + bean.getOrderInfo().getRemarks());
        if (bean.getOrderInfo().getOrderState().equals("1")) {
            mOrderState.setText("订单状态：待付款");
        } else if (bean.getOrderInfo().getOrderState().equals("2")) {
            mOrderState.setText("订单状态：待接单");
            mEndBtn.setVisibility(View.GONE);
        } else if (bean.getOrderInfo().getOrderState().equals("3") || bean.getOrderInfo().getOrderState().equals("4")) {
            mOrderState.setText("订单状态：进行中");
            mCancleBtn.setVisibility(View.VISIBLE);
        } else if (bean.getOrderInfo().getOrderState().equals("5")) {
            mOrderState.setText("订单状态：已完成");
            mCancleBtn.setVisibility(View.GONE);
            mEndBtn.setVisibility(View.VISIBLE);
        } else if (bean.getOrderInfo().getOrderState().equals("7")) {
            mOrderState.setText("订单状态：审核中");
            mCancleBtn.setVisibility(View.GONE);
            mEndBtn.setVisibility(View.GONE);
        } else if (bean.getOrderInfo().getOrderState().equals("8")) {
            mOrderState.setText("订单状态：已取消");
            mCancleBtn.setVisibility(View.GONE);
            mEndBtn.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);

    }

    @Override
    public void cancelSuccess() {
        ShowToast.showToast("取消成功");
        finish();
        RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);
    }

    @Override
    public void finishSuccess() {
        if (getSharedPreference().getString("userType", "1").equals(1)) {
            if ((mCOrderState.equals("3") && mUserOrderState != null) || mCOrderState.equals("5")) {
                jumpToEvaluate();//跳转评价
            } else {
                ShowToast.showToast("暂不可以评价");
            }
        } else {
            if ((mCOrderState.equals("3") && mRelUserOrderState != null) || mCOrderState.equals("5")) {
                jumpToEvaluate();
            } else {
                ShowToast.showToast("暂不可以评价");
            }
        }

    }

    /**
     * 跳转评价页面
     */
    private void jumpToEvaluate() {
        RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);
        ShowToast.showToast("结束成功，请评价");
        Intent intent = new Intent(this, EvaluateActivity.class);
        KLog.i("orderId=" + mOrderId);
        intent.putExtra("orderId", mOrderId);
        startActivity(intent);
    }

}
